<template>
  <div class="MessageManagement" v-loading="loading">
    <el-form :inline="true" :model="searchData">
      <el-form-item label="留言状态：">
        <el-select v-model="searchData.status" placeholder="请选择状态">
          <el-option label="已回复" :value="1"></el-option>
          <el-option label="未回复" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键字：">
        <el-input
          v-model="searchData.keywords"
          placeholder="请输入关键字"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="serach"
          class="btn-search"
          >筛选</el-button
        >
        <el-button icon="el-icon-refresh-left" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      style="width: 100%"
      height="84%"
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
    >
      <el-table-column type="index" label="序号" align="center">
      </el-table-column>
      <el-table-column prop="state" label="留言状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.state ? "已回复" : "未回复" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="messageContent" label="留言内容" align="center">
      </el-table-column>
      <el-table-column prop="messageTime" label="留言时间" align="center">
      </el-table-column>
      <el-table-column prop="messageUserName" label="留言人" align="center">
      </el-table-column>
      <el-table-column prop="replyTime" label="回复时间" align="center">
      </el-table-column>
      <el-table-column prop="replyUserName" label="回复人" align="center">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button
            @click="handleClick(scope.row)"
            type="text"
            size="medium"
            style="margin-right: 10px"
            >回复</el-button
          >
          <el-popconfirm
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @confirm="deleteConfirm(scope.row)"
          >
            <el-button
              slot="reference"
              type="text"
              size="medium"
              style="color: red"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="MessageManagement-content-page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="回复"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form :model="formData" :rules="formRules" ref="form">
        <div class="info-box">
          <div class="info-item">
            <p class="name">留言内容：</p>
            <p class="value">{{ formData.messageContent }}</p>
          </div>
          <div class="info-item">
            <p class="name">留言人：</p>
            <p class="value">{{ formData.messageUserName }}</p>
          </div>
          <div class="info-item" v-if="formData.state">
            <p class="name">回复内容：</p>
            <p class="value">{{ formData.replyContent }}</p>
          </div>
        </div>
        <el-form-item label="回复内容：" prop="content" v-if="!formData.state">
          <el-input
            type="textarea"
            maxlength="100"
            rows="2"
            show-word-limit
            v-model="formData.content"
            placeholder="请输入回复内容"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    //弹窗的输入框校验
    const validateContent = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入回复内容"));
      } else {
        callback();
      }
    };
    return {
      //加载状态
      loading: false,
      //分页数据
      currentPage: 1,
      pageSize: 10,
      total: 0,
      searchData: {
        status: "",
        keywords: "",
      },
      tableData: [],
      formData: {},
      dialogVisible: false,
      //弹窗校验规则
      formRules: {
        content: [
          { required: true, trigger: "blur", validator: validateContent },
        ],
      },
    };
  },
  mounted() {
    this.getMessageList();
  },
  methods: {
    //获取数据
    async getMessageList(state, messageType, keyWords) {
      this.loading = true;
      const res = await this.$api.getMessageList({
        pageIndex: this.currentPage,
        pageSize: this.pageSize,
        state: state,
        messageType: messageType,
        keyWords: keyWords,
      });
      // console.log(res.data);
      this.tableData = res.data.records;
      this.total = res.data.total;
      this.loading = false;
    },
    //查询数据
    serach() {
      this.getMessageList(
        this.searchData.status,
        this.searchData.sort,
        this.searchData.keywords
      );
    },
    //重置清空筛选条件
    reset() {
      this.searchData = {
        status: "",
        keywords: "",
      };
      this.getMessageList();
    },
    //弹窗确认
    async confirm() {
      // console.log(this.formData, "formData");
      let user = JSON.parse(localStorage.getItem("userInfo"));
      if (this.formData.replyContent) {
        this.dialogVisible = false;
        return;
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$api
            .updateMessage({
              id: this.formData.id,
              replyContent: this.formData.content,
              replyUserId: user.id, //回复人id
            })
            .then((res) => {
              this.loading = false;
              this.dialogVisible = false;
              this.reset();
            });
        } else {
          this.$message({
            message: "请输入回复内容",
            type: "error",
          });
          return;
        }
      });
    },
    //删除
    async deleteConfirm(row) {
      const res = await this.$api.deleteMessageById({
        id: row.id,
      });
      this.serach();
      if (res.code === 0) {
        this.serach();
        this.$message({
          message: "删除成功！",
          type: "success",
        });
      }
    },
    //操作打开弹窗
    handleClick(row) {
      // console.log(row);
      this.formData = row;
      this.dialogVisible = true;
    },
    //关闭弹窗的方法
    handleClose() {
      this.dialogVisible = false;
    },
    //页数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      let that = this;
      that.pageSize = val;
      that.serach();
    },
    //翻页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      let that = this;
      that.currentPage = val;
      that.serach();
    },
  },
};
</script>

<style lang="scss" scoped>
.btn-search {
  &:hover {
    opacity: 0.8;
  }
}
.btn-export {
  width: 100px;
  margin-bottom: 20px;
  &:hover {
    opacity: 0.8;
  }
}
.info-box {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 20px;
  .info-item {
    display: flex;
    align-items: center;
    gap: 20px;
    .name {
      font-size: 16px;
      font-weight: bold;
    }
    .vlaue {
      font-size: 14px;
    }
  }
}
::v-deep(.el-form-item__content) {
  flex: 1;
}

.MessageManagement {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  background-color: #ffffff;
  border-radius: 4px;
  padding: 24px;
  overflow: hidden;
  .flex-item-start {
    width: max-content;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-direction: row;
    label {
      font-size: 16px;
      color: rgba(85, 85, 85, 0.996078431372549);
    }
  }
  .myInput {
    width: 248px;
    // margin: 24px;
  }
  .mySelect {
    width: 248px;
  }
  &-top {
    width: 100%;
    display: flex;
    flex-direction: column;
    margin-bottom: 16px;
    &-search {
      display: flex;
      flex-direction: row;
      margin-bottom: 16px;
    }
  }
  &-content {
    width: 100%;
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    &-table {
      width: 100%;
      flex: 1;
      overflow-y: auto;
    }
    &-page {
      display: flex;
      justify-content: flex-end;
      width: 100%;
      margin-top: 16px;
      .el-pagination {
        padding: 0;
        .el-pagination__total {
          line-height: 40px;
          font-size: 16px;
          color: rgba(0, 0, 0, 0.8);
        }
      }
    }
  }
}
</style>